classdef MarkerDetection
    %MARKERDETECTION Summary of this class goes here
    %   Detailed explanation goes here
    
    properties(SetAccess = public, GetAccess = public, Access = public)
        m_point_tracker = [1 1];
        m_point_tracker_out;
        m_graytmp;
        m_act_frame1;
        
        m_output_mk = [1 1];
    end
    
    methods
        function obj = MarkerDetection(point_tracker, graytmp)
            obj.m_point_tracker = [point_tracker];
            obj.m_graytmp = rgb2gray(graytmp);
        end
        
        function obj = detectMarker(obj, act_frame1)
            grayimg = rgb2gray(act_frame1);
            
            erg_template = templateMatching(grayimg, obj.m_graytmp);
            [Treshold, point_tracker1] = max(erg_template);
            [Treshold, point_tracker2] = max(Treshold);
            
            obj.m_point_tracker = [point_tracker1(point_tracker2), point_tracker2];
        end
            
        function obj = updateMarker(obj, act_frame1)
            
            obj.m_act_frame1= act_frame1;
            grayimg = rgb2gray(act_frame1);
            
            starttracking = [round(max(1, obj.m_point_tracker(1)-(size(obj.m_graytmp,1)*1.125))),               round(max(1, obj.m_point_tracker(2)-(size(obj.m_graytmp,2)*1.125)))];
            endtracking = [round(min(size(grayimg,1), obj.m_point_tracker(1)+(size(obj.m_graytmp,1)*1.125))),   round(min(size(grayimg,2), obj.m_point_tracker(2)+(size(obj.m_graytmp,2)*1.125)))];
            partimg = grayimg(starttracking(1): endtracking(1),starttracking(2): endtracking(2));
            
            erg_template = templateMatching(partimg, obj.m_graytmp);
            [Treshold, point_tracker1] = max(erg_template);
            [Treshold, point_tracker2] = max(Treshold);
            point_tracker = [point_tracker1(point_tracker2), point_tracker2];
            obj.m_point_tracker(1) = point_tracker(1)-1+starttracking(1);
            obj.m_point_tracker(2) = point_tracker(2)-1+starttracking(2);
            
            obj.m_output_mk = [obj.m_point_tracker(2) obj.m_point_tracker(2)+size(obj.m_graytmp,2)];
            
            % thresholds
            %TH_VP = 50;
            %TH_HP = 400;
            TH_VP = 25;
            TH_HP = 200;

            I = rgb2i1i2i3(act_frame1);  % convert to I1I2I3 color space

            board = ~im2bw(I(:,:,3));       % using I3 channel for segmentation
            %board = bwareaopen(board, 30000, 8);
            board = bwareaopen(board, 5000, 8);
            b = board;          % for field 20 or 11
            board = bwfill(board, 'holes');
            vp = sum(board);    % vertical profile
            board(:, vp<TH_VP) = 0;
            hp = sum(board,2);  % horizontal profile
            board(hp<TH_HP, :) = 0; 

            [xx, hfir] = find(board, 1, 'first');
            [xx, hlast] = find(board, 1, 'last');
                                    
             obj.m_point_tracker_out = [hfir hlast];
        end
        
        function drawMarkerOutside(obj)                     
            Xv = [obj.m_point_tracker_out(1,1) obj.m_point_tracker_out(1,2); obj.m_point_tracker_out(1,1) obj.m_point_tracker_out(1,2)];
            Yv = [1 1; size(obj.m_act_frame1, 2) size(obj.m_act_frame1,2)];
            
            hold on;
            line(Xv,Yv,  'Color', 'r');
            hold off;           
            
        end
        
        function drawMarker(obj)
            %line([obj.m_point_tracker(2),obj.m_point_tracker(2)+size(obj.m_graytmp,2)],[obj.m_point_tracker(1),obj.m_point_tracker(1)+size(obj.m_graytmp,1)],'Marker','.','LineStyle','-')
            
            Xv = [obj.m_output_mk(1,1) obj.m_output_mk(1,2); obj.m_output_mk(1,1) obj.m_output_mk(1,2)];
            Yv = [1 1; size(obj.m_act_frame1, 2) size(obj.m_act_frame1,2)];
            
            hold on;
            line(Xv,Yv,  'Color', 'r');
            hold off;      
        end
        
        function [X W] = getBoundingBox(obj)
           X = [obj.m_point_tracker(1), obj.m_point_tracker(2)];
           W = [size(obj.m_graytmp, 1), size(obj.m_graytmp, 2)];
        end
    end
    
end

